<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="13">
          <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password" placeholder="请输入密码密码" clearable
                      prefix-icon='el-icon-s-tools' show-password :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="13">
          <el-form-item label="重复密码" prop="password2">
            <el-input v-model="formData.password2" placeholder="请重复输入密码" clearable
                      prefix-icon='el-icon-s-tools' show-password :style="{width: '100%'}"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
import { updatePassword } from '@/api/user'

export default {
  components: {},
  props: [],
  data() {
    return {
      formData: {
        password: "",
        password2: ""
      },
      rules: {
        password: [{
          required: true,
          message: '请输入密码密码',
          trigger: 'blur'
        }],
        password2: [{
          required: true,
          message: '请重复输入密码',
          trigger: 'blur'
        }],
      },
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      let data = this.formData
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        if(data.password != data.password2){
          this.$message.error('两次输入密码不一致')
          return
        }
        updatePassword({password:data.password}).then(res=>{
          this.$message.success('修改密码成功')
          this.$store.dispatch('user/logout')
          this.$router.push(`/login?redirect=${this.$route.fullPath}`)
        }).catch(err=>{
          this.$message.error('服务异常')
        })
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    },
  }
}

</script>
<style>
</style>
